package com.turato.json.myjson;

import java.util.ArrayList;

/**
 * @author tuhao
 * @desc json 数组
 */
public class JsonArray extends ArrayList<Object> {


    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("[");
        boolean f = false;
        for(Object object : this){
            if(f) {
                sb.append(',');
            }
            sb.append(StringUtils.toJsonString(object));
            f = true;
        }
        return sb.append(']').toString();
    }

    public String toStringUnicode() {
        StringBuilder sb = new StringBuilder("[");
        boolean f = false;
        for(Object object : this){
            if(f) {
                sb.append(',');
            }
            sb.append(StringUtils.toUnicodeString(StringUtils.toJsonString(object)));
            f = true;
        }
        return sb.append(']').toString();
    }

    public <T> T get(int key, Class<T> type) {
        //反射
        return type.cast(get(key));
    }

    public String getString(int key) {
        return get(key, String.class);
    }

    public JsonObject getObject(int key){
        return get(key, JsonObject.class);
    }

    public JsonArray getArray(int key){
        return get(key, JsonArray.class);
    }

}
